package com.sopaths.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;


public class PaginationTag extends SimpleTagSupport {

	private int currentPage;

	private int totalPage;

	private String href;

	private final int prefixPage = 4;

	private final int suffixPage = 4;

	@Override
	public void doTag() throws JspException, IOException {
		validate();

		StringBuilder paginationHtml = new StringBuilder();
		paginationHtml.append("<ul style=\"margin-left:0; padding-left:0; list-style-type:none; text-decoration:none;\">");
		if (currentPage > 1) {
			paginationHtml.append("<li style=\"display:inline;\"><a href=\"" + href.replace("{page}", currentPage - 1 + "") + "\" style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none;\">上一页</a></li>");
		}
		int startPage = currentPage - prefixPage >= 1 ? currentPage - prefixPage : 1;
		int endPage = currentPage + suffixPage <= totalPage ? currentPage + suffixPage : totalPage;
		if (startPage > 1) {
			paginationHtml.append("<li style=\"display:inline;\"><a href=\"" + href.replace("{page}", 1 + "") + "\" style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none;\">1</a></li>");
		}
		if (startPage > 2) {
			paginationHtml.append("<li style=\"display:inline;\"><a style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none;\">...</a></li>");
		}
		for (int i = startPage; i <= endPage; i++) {
			paginationHtml.append("<li style=\"display:inline;\"><a href=\"" + href.replace("{page}", i + "") + "\" style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none; " + (i == currentPage ? "border-bottom:solid 1px #A5AD8C; font-weight:bold; " : "") + "\">" + i + "</a></li>");
		}
		if (endPage < totalPage - 1) {
			paginationHtml.append("<li style=\"display:inline;\"><a style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none;\">...</a></li>");
		}
		if (endPage < totalPage) {
			paginationHtml.append("<li style=\"display:inline;\"><a href=\"" + href.replace("{page}", totalPage + "") + "\" style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none;\">" + totalPage + "</a></li>");
		}
		if (currentPage < totalPage) {
			paginationHtml.append("<li style=\"display:inline;\"><a href=\"" + href.replace("{page}", currentPage + 1 + "") + "\" style=\"display:inline-block; padding: 0 5px 0 5px; color:#A5AD8C; text-decoration:none;\">下一页</a></li>");
		}
		JspWriter out = getJspContext().getOut();
		out.println(paginationHtml.toString());
	}

	private void validate() {
		if (currentPage <= 0) {
			throw new IllegalArgumentException("Property currentPage cannot less than zero. " + currentPage);
		}
		if (totalPage <= 0) {
			throw new IllegalArgumentException("Property totalPage cannot less than zero. " + totalPage);
		}
		if (currentPage > totalPage) {
			throw new IllegalArgumentException("Property currentPage cannot exceed totalPage. currentPage: " + currentPage + ". totalPage: " + totalPage);
		}
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}
}
